package cn.com.mayue.guava.basic;

import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

/**
 * Created by mayue on 2016/12/4.
 */
public class MoreObjectsTest {
    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Test
    public void testStringHelper() {
        System.out.println(MoreObjects.toStringHelper(new MoreObjectsTest()).add("x", 1));
        System.out.println(MoreObjects.toStringHelper(MoreObjectsTest.class.getName()).add("x", 1).add("y", 2));

    }


    @Test
    public void testFirstNonNull() {
        //不为空  则赋值
        String tempStr = null;
        String rltStr;
        if (StringUtils.isNotEmpty(tempStr)) {
            rltStr = tempStr;
        } else {
            rltStr = "1";
        }
        System.out.println(StringUtils.center(rltStr, 10, "="));
        //使用 apache common
        System.out.println(StringUtils.center(StringUtils.defaultIfEmpty(tempStr, "1"), 10, "#"));
        //使用guba
        System.out.println(StringUtils.center(MoreObjects.firstNonNull(tempStr, "1"), 10, "*"));
    }

    @Test
    public void testFirstNonNullTwo(){
        System.out.println(MoreObjects.firstNonNull(Optional.fromNullable(""), "1"));
    }


}
